// stencil2word.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>

#include "ImageSplitter.h"
#include "WordProxy.h"
#include "Utils.h"
#include "FileNameUtils.h"

#include "ProgramOptions.h"

#include "Export.h"

#include <iostream>
using namespace std;

LPCTSTR cPrefix = _T("image%04d."); // TODO: Prefix should be like input file name

int _tmain(int argc, _TCHAR* argv[])
{
	CProgramOptions Options;
	if( Options.ParseCommandLine(argc, argv) )
	{
		tstring OutputPath;
		if( !CBaseExport::GetOutputPath(Options, OutputPath) )
			return 0;

		CImageSplitter Splitter;
		if( !CBaseExport::LoadAndResizeImage(Splitter, Options) )
			return 0;
		
		if( Options.GetExportType() == exp2Word)
		{
			CWordExport::ExportToWord(Splitter, OutputPath);
		}
		else if (Options.GetExportType() == exp2File)
		{
			SIZE PieceSize = {0};
			tstring OutputFileNamePattern = cPrefix;
			int MaxFileNameLen = 0;

			tstring InputPath;
			Options.GetInputFileName(InputPath);

			if( Options.NeedSplit() )
			{
				Options.GetPieceSize(PieceSize);
				
				tstring OutputFileExt;
				
				if( HasJPEGExt(InputPath) )
				{
					Splitter.SetOutputImageType(imtJPEG);
					GetDefaultJPEGExt(OutputFileExt);
				}
				else if( HasPNGExt(InputPath) )
				{
					Splitter.SetOutputImageType(imtPNG);
					GetDefaultPNGExt(OutputFileExt);
				}
				OutputFileNamePattern.append(OutputFileExt);
				MaxFileNameLen = OutputFileNamePattern.length() + 1;
			}
			else
			{
				if( HasJPEGExt(OutputPath) )
					Splitter.SetOutputImageType(imtJPEG);
				else if( HasPNGExt(OutputPath) )
					Splitter.SetOutputImageType(imtPNG);
			}

			CFileExport::ExportToFile(Splitter, OutputPath, PieceSize, OutputFileNamePattern, MaxFileNameLen);
		}
		else
			_ASSERT( false );
		Splitter.Close();
	}
	return 0;
}

